import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/models/deviceinfomodel.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';
import 'package:flutter/cupertino.dart';

class TemandHumPage extends StatefulWidget {
  const TemandHumPage({Key key}) : super(key: key);

  @override
  _TemandHumPageState createState() => _TemandHumPageState();
}

class _TemandHumPageState extends State<TemandHumPage> with SingleTickerProviderStateMixin{
  String path = 'assets/images/common/device/d-an.png';
  String path2 = 'assets/images/common/device/d-liang.png';

  Animation<double> animation;
  AnimationController _controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
        _controller = new AnimationController(
        duration: const Duration(milliseconds: 1000), vsync: this);
    animation = new Tween(begin: 20.0, end: 35.0).animate(_controller)
      ..addListener(() {
        setState(() {
          // the state that has changed here is the animation object’s value
        });
      });
    animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        _controller.forward();
      }
    });
    _controller.forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(255, 250, 212, 1),
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.transparent,
        leading: InkWell(
          onTap: () {
            Navigator.pop(context);
          },
          child: Icon(
            Icons.keyboard_arrow_left,
            color: Color.fromRGBO(255, 255, 255, 1),
          ),
        ),
      ),
      body: Column(
        children: [
          Container(
            height: 300,
            child: Stack(
              alignment: Alignment.center,
              children: [
                Positioned(
                    // top: animation.value,
                    top: animation.value,
                    child: Container(
                        child:
                            Image.asset('assets/images/common/device/湿度器.png',width: 300,))),
              ],
            ),
          ),
          Expanded(
            child: ListView(
              children: _devicelist(),
            ),
          ),
        ],
      ),
    );
  }

  _devicelist() {
    List<Widget> list = [];
    List devicelist = context.watch<DeviceInfomodel>().getlistbyType(7);
    print(devicelist.length);
    for (int i = 0; i < devicelist.length; i++) {
      list.add(_buildleditem(devicelist[i]));
    }
    return list;
  }

  _buildleditem(Deviceitemmodel deviceitemmodel) {
    double temp = double.parse(deviceitemmodel.content.split(';')[0].split(':')[1]);
    double hum = double.parse(deviceitemmodel.content.split(';')[1].split(':')[1]);
    return Container(
      margin: EdgeInsets.fromLTRB(8, 8, 8, 8),
      width: 300,
      height: 100,
      decoration: new BoxDecoration(
        boxShadow: [
          BoxShadow(
              color: Color.fromRGBO(255, 255, 255, 0.3),
              offset: Offset(0.0, 0.0), //阴影xy轴偏移量
              blurRadius: 20.0, //阴影模糊程度
              spreadRadius: 0.5 //阴影扩散程度
              )
        ],
        color:  Color.fromRGBO(255, 255, 255, 1),
        //设置四周圆角 角度
        borderRadius: BorderRadius.all(Radius.circular(10.0)),
        //设置四周边框
        // border: new Border.all(width: 1, color: Colors.red),
      ),
      child: Row(
        // mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          // Image.asset(path3),
          Padding(
            padding: const EdgeInsets.all(32.0),
            child: SvgPicture.asset(
              "assets/images/icons/svg/温湿度表.svg",
              // color: Colors.white,
              width: 32,
              
            ),
          ),
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              
              children: [
                Text(
                  // widget.name,
                  deviceitemmodel.name,
                  style: TextStyle(color: Colors.black87, fontSize: 22),
                ),
                Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(
                      '工作状态:',
                      style: TextStyle(color: Colors.black87, fontSize: 14),
                    ),
                    Text(
                      '温度:$temp 湿度:$hum',
                      style: TextStyle(color: Colors.black54, fontSize: 14),
                    ),
                  ],
                )
              ],
            ),
          ),
          // CupertinoSwitch(
          //   trackColor:  Color.fromRGBO(255, 250, 212, 1),
          //   activeColor:  Colors.blueGrey[600],
          //   value: state,
          //   onChanged: (bool value) {
          //     print(value);
          //     if(value){
          //        context.read<DeviceInfomodel>().operatedevice(deviceitemmodel.typeId, deviceitemmodel.deviceId, 'turnon');
          //     }
          //     else {
          //       context.read<DeviceInfomodel>().operatedevice(deviceitemmodel.typeId, deviceitemmodel.deviceId, 'turnoff');
          //     }
          //     setState(() {
          //       // this.state = value;
          //       // if (value) {
          //       //   this.state = 1;
          //       //   // _turnon();
          //       // } else {
          //       //   this.state = 0;
          //       //   _turnoff();
          //       // }

          //     });
          //   },
          // ),
        ],
      ),
    );
  }
}
